#include "processtask.h"
#include "preprocessor.h"
#include "logger.h"
#include "aiprocess.h"
#include "cvprocessor.h"
#include "cvalgedgedetection.h"
#include "pccore.h"

ProcessTask::ProcessTask(ImageRecord &imgcard, StepProcess endstep, QObject *parent)
    : QObject(parent), endstep(endstep), imgcard(imgcard)
{
    setAutoDelete(true);
}

void ProcessTask::run()
{
    ImageProcessor::getInstance().multithreadingAdd();
    LogTrack << "go in ProcessTask ......";
    if (endstep == Capture || imgcard.getStepImage(Capture).empty())
    {
        if(imgcard.getStepImage(Capture).empty())
        {
            imgcard.setDefect(8);
        }
        imgcard.setIsSucceeded(true);
        emit finishProcess(imgcard);
        ImageProcessor::getInstance().multithreadingsub();
        return;
    }

    // try{
    LogTrack << "go in PreProcessor ......";
    PreProcessor::getInstance().processImg(imgcard);
    if (imgcard.getState() == ImageRecord::Fail)
    {
        imgcard.setIsSucceeded(false);
        emit finishProcess(imgcard);
        ImageProcessor::getInstance().multithreadingsub();
        return;
        // throw "PreProcessor End";
    }
    // }catch (const std::exception& e) {
    //     imgcard.setIsSucceeded(false);
    //     LogWarning<< "fail ProcessTask in preprocessing ";
    //     emit errorOccurred("An error occurred in the task : preprocessing");
    //     emit finishProcess(imgcard);
    //     return;
    // }
    if (endstep == Reprogress)
    {
        LogTrack << "step arrived Reprogress Finish Task";
        imgcard.setIsSucceeded(true);
        emit finishProcess(imgcard);
        ImageProcessor::getInstance().multithreadingsub();
        return;
    }

    // detecet检测
    // try{
    //     PreProcessor::getInstance().processImg(imgcard);
    // }catch (const std::exception& e) {
    //     emit errorOccurred("An error occurred in the task : preprocessing");
    // }
    LogTrack << "step to Detect Toal Task << " << endstep;
    if (endstep == Detect || endstep == total)
    {
        AIprocess::getInstance().processImg(imgcard);
        LogTrack << "step arrived AI Finish Task";
        if (endstep == Detect)
        {
            imgcard.setIsSucceeded(true);
            emit finishProcess(imgcard);
            ImageProcessor::getInstance().multithreadingsub();
            return;
        }
    }

    if (endstep == Cv ||
        endstep == total ||
        ImageProcessor::getInstance().selectedStep() == ImageProcessor::MeauPCcore )
    {
        CvAlgEdgeDetection::getInstance().process(imgcard);

        LogTrack << "step arrived CvProcessor Finish Task";
        if (endstep == Cv)
        {
            imgcard.setIsSucceeded(true);
            emit finishProcess(imgcard);
            ImageProcessor::getInstance().multithreadingsub();
            return;
        }

        PCcore::getInstance().process(imgcard);

        if(ImageProcessor::getInstance().selectedStep() == ImageProcessor::MeauPCcore)
        {
            imgcard.setIsSucceeded(true);
            emit finishProcess(imgcard);
            ImageProcessor::getInstance().multithreadingsub();
            return;
        }
    }

    if (endstep == Summary)
    {
        imgcard.setIsSucceeded(true);
        emit finishProcess(imgcard);
        ImageProcessor::getInstance().multithreadingsub();
        return;
    }

    if (endstep == total)
    {
        LogTrack << "finshi process endstep == total";
        imgcard.setIsSucceeded(true);
        emit finishProcess(imgcard);
        ImageProcessor::getInstance().multithreadingsub();
        return;
    }

    emit finishProcess(imgcard);
    ImageProcessor::getInstance().multithreadingsub();
}
